/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int findTilt(TreeNode* root) {
        if(root==nullptr){return 0;}
        int ans=0;
        queue<TreeNode*> que;
        que.push(root);
        TreeNode* tmp;
        while(!que.empty()){
            tmp=que.front();que.pop();
            ans+=abs(sumnode(tmp->left)-sumnode(tmp->right));
            if(tmp->left){que.push(tmp->left);}
            if(tmp->right){que.push(tmp->right);}
        }
        return ans;
    }
    int sumnode(TreeNode* root){
        if(root==nullptr){return 0;}
        return root->val+sumnode(root->left)+sumnode(root->right);
    }
};